﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WenSkin.Controls;

namespace WenPostTool.Controls
{
    public class HistoryControl : WenSkin.Controls.WenControl
    {
        public HistoryControl() : base()
        {
            Size = new System.Drawing.Size(202, 202);
            flow = new FlowLayoutPanel()
            {
                Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right,
                Size = new System.Drawing.Size(200, 200),
                Location = new System.Drawing.Point(1, 1),
                FlowDirection = FlowDirection.LeftToRight,
                AutoScroll = true,
            };

            flow.SizeChanged += Flow_SizeChanged;
            this.Controls.Add(flow);
        }

        private void Flow_SizeChanged(object sender, EventArgs e)
        {
            foreach (Control item in flow.Controls)
            {
                item.Width = flow.Width - 30;
            }
        }
        #region 私有属性
        private readonly FlowLayoutPanel flow;
        private HistoryItemcollection items;
        #endregion


        #region  公有属性
        [Category("Wen")]
        [Description("选项集合")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public HistoryItemcollection Items => items ??= new HistoryItemcollection(this);
        #endregion


        #region 委托

        public event PostClickEventHandler DeleteClick;
        public event PostClickEventHandler ItemClick;


        public delegate void PostClickEventHandler(object sender, PostClickEventArgs e);
        public class PostClickEventArgs : EventArgs
        {
            private readonly HistoryItem historyItem;

            public PostClickEventArgs(HistoryItem historyItem)
            {
                this.historyItem = historyItem;
            }

            public HistoryItem HistoryItem { get => historyItem; }
        }

        private void OnDeleteClick(HistoryItem historyItem)
        {
            DeleteClick?.Invoke(this, new PostClickEventArgs(historyItem));
        }       
        private void OnItemClick(HistoryItem historyItem)
        {
            ItemClick?.Invoke(this, new PostClickEventArgs(historyItem));
        }
        #endregion



        public class HistoryItemcollection : WenCollection
        {
            private HistoryControl ower;

            public HistoryItemcollection(HistoryControl ower)
            {
                this.ower = ower;
            }

            public HistoryItem this[int index] => Items[index] as HistoryItem;

            public override int Add(object value)
            {
                if (value is HistoryItem item)
                {
                    HistoryItemControl historyItemControl = new HistoryItemControl(item, ower);
                    ower.flow.Controls.Add(historyItemControl);
                    ower.flow.Controls.SetChildIndex(historyItemControl, 0);
                }
                return base.Add(value);
            }
            public override void RemoveAt(int index)
            {
                ower.flow.Controls.RemoveAt(index);
                base.RemoveAt(index);
            }
            public override void Clear()
            {
                ower.flow.Controls.Clear();
                base.Clear();
            }
        }

        [ToolboxItem(false)]
        public class HistoryItemControl : WenControl
        {
            private HistoryItem item { get; }
            private HistoryControl historyControl { get; }

            public HistoryItemControl(HistoryItem item, HistoryControl historyControl) : base()
            {

                this.item = item;
                this.historyControl = historyControl;

                Size = new System.Drawing.Size(historyControl.flow.Width - 30, 30);
                WenLable wenLable = new WenLable
                {
                    Text = item.Post,
                    Width = 40,
                    Dock = DockStyle.Left,
                    BackColor=Color.Transparent,
                };
                wenLable.MouseEnter += WenLable_MouseEnter;
                wenLable.MouseLeave += WenLable_MouseLeave;
                wenLable.Click += WenLable_Click;

                WenLable urlLable = new WenLable()
                {
                    Text = item.Url,
                    Dock = DockStyle.Fill,
                    BackColor = Color.Transparent,
                };
                urlLable.MouseEnter += WenLable_MouseEnter;
                urlLable.MouseLeave += WenLable_MouseLeave;
                urlLable.Click += WenLable_Click;

                WenButton wenButton = new WenButton()
                {
                    Text = "删除",
                    Width = 30,
                    Dock = DockStyle.Right,
                    BackColor = Color.Transparent,
                };
                wenButton.Click += (s, e) =>
                {
                    historyControl.flow.Controls.Remove(this);
                    historyControl.OnDeleteClick(item);
                };

                this.Controls.Add(urlLable);
                this.Controls.Add(wenLable);
                this.Controls.Add(wenButton);

            }

            private void WenLable_Click(object sender, EventArgs e)
            {
                this.historyControl.OnItemClick(this.item);
            }

            private void WenLable_MouseLeave(object sender, EventArgs e)
            {
                this.BackColor = Color.Transparent;
            }

            private void WenLable_MouseEnter(object sender, EventArgs e)
            {
                this.BackColor = Color.FromArgb(63,63,70);
            }
        }
    }
}
